package zqwl_1dxrr16

import (
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/context"
	"time"
)

type response struct {
	ctx    context.Context
	Msg    string `json:"msg"`
	Status bool   `json:"status"`
	Data   any    `json:"data,omitempty"`
}

func (r response) success() {
	r.Status = true
	if r.Msg == "" {
		r.Msg = "操作成功"
	}
	_, _ = r.ctx.JSON(r)
}

func (r response) fail() {
	r.Status = false
	_, _ = r.ctx.JSON(r)
}

func InitWeb(relay Relay, config *Config) {
	web := iris.New()
	r := web.Party("/api/relay")
	{
		r.Get("/test", func(c context.Context) {
			response{ctx: c, Data: []int{1, 2, 3, 4, 5}}.success()
		})
		r.Get("/on-all", func(c context.Context) {
			err := relay.OnAll()
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("打开继电器失败: %s", err)}.fail()
				return
			}
			response{ctx: c}.success()
		})

		r.Get("/on-all/{reset_time}", func(c context.Context) {
			resetTime, err := c.Params().GetInt("reset_time")
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("获取继电器重置失败: %s", err)}.fail()
			}
			err = relay.OnAll()
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("打开继电器失败: %s", err)}.fail()
				return
			}
			time.AfterFunc(time.Second*time.Duration(resetTime), func() {
				err := relay.OffAll()
				if err != nil {
				}
			})
			response{ctx: c}.success()
		})

		r.Get("/on/{branch}", func(c context.Context) {
			branch, err := c.Params().GetInt("branch")
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("获取继电器路数失败: %s", err)}.fail()
			}
			err = relay.On(byte(branch))
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("打开继电器失败: %s", err)}.fail()
				return
			}
			response{ctx: c}.success()
		})

		r.Get("/on/{branch}/{reset_time}", func(c context.Context) {
			branch, err := c.Params().GetInt("branch")
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("获取继电器路数失败: %s", err)}.fail()
			}
			resetTime, err := c.Params().GetInt("reset_time")
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("获取继电器重置失败: %s", err)}.fail()
			}
			err = relay.On(byte(branch))
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("打开继电器失败: %s", err)}.fail()
				return
			}
			time.AfterFunc(time.Second*time.Duration(resetTime), func() {
				err := relay.Off(byte(branch))
				if err != nil {
				}
			})
			response{ctx: c}.success()
		})

		r.Get("/off-all", func(c context.Context) {
			err := relay.OffAll()
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("关闭继电器失败: %s", err)}.fail()
				return
			}
			response{ctx: c}.success()
		})

		r.Get("/off/{branch}", func(c context.Context) {
			branch, err := c.Params().GetInt("branch")
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("获取继电器路数失败: %s", err)}.fail()
			}
			err = relay.Off(byte(branch))
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("打开继电器失败: %s", err)}.fail()
				return
			}
			response{ctx: c}.success()
		})

		r.Get("/status", func(c context.Context) {
			status, err := relay.Status()
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("获取继电器状态失败: %s", err)}.fail()
				return
			}
			var data = make([]int, len(status))
			for i, b := range status {
				data[i] = int(b)
			}
			response{ctx: c, Data: data}.success()
		})

		r.Get("/info", func(c context.Context) {
			info, err := relay.Info()
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("获取继电器信息失败: %s", err)}.fail()
				return
			}
			response{ctx: c, Data: info}.success()
		})

		r.Get("/setting", func(c context.Context) {
			config.Address = c.URLParamDefault("address", "COM19")
			relay.Close()
			r, err := NewRTUClientHandler(*config)
			if err != nil {
				response{ctx: c, Msg: fmt.Sprintf("连接继电器失败: %s", err)}.fail()
				return
			}
			relay = r
			response{ctx: c}.success()
		})
	}
	return
}
